package com.jiyun.h2303b.service.impl;

import com.github.wxpay.sdk.WXPayUtil;
import com.jiyun.h2303b.service.WxPayService;
import com.jiyun.h2303b.util.HttpClient;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxPayServiceImpl implements WxPayService {
    @Override
    public Map getCodeUrl(String out_trade_no,String total_fee) throws Exception {
        // 封装参数，只封装必填参数，选填的根据实际的业务场景去添加
        Map map = new HashMap();
        map.put("appid","wxab8acb865bb1637e");
        map.put("mch_id","11473623");
        map.put("nonce_str", WXPayUtil.generateNonceStr());
        map.put("body","这是一个商品");
        map.put("out_trade_no",out_trade_no);
        map.put("total_fee",total_fee);
        map.put("spbill_create_ip","127.0.0.1");
        map.put("notify_url","127.0.0.1");
        map.put("trade_type","NATIVE");
        String xmlParam = WXPayUtil.generateSignedXml(map,"2ab9071b06b9f739b950ddb41db2690d");
        System.out.println(xmlParam);
        // 请求接口
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
        httpClient.setHttps(true);
        httpClient.setXmlParam(xmlParam);
        httpClient.post();
        String result = httpClient.getContent();
        // 处理返回结果
        System.out.println(result);
        Map<String, String> resultMap =  WXPayUtil.xmlToMap(result);
//        String codeUrl =  resultMap.get("code_url");
        resultMap.put("out_trade_no",out_trade_no);
        resultMap.put("total_fee",total_fee);
        return resultMap;
    }

    @Override
    public String orderquery(String out_trade_no) throws Exception {
        // 封装参数
        Map map = new HashMap();
        map.put("appid","wxab8acb865bb1637e");
        map.put("mch_id","11473623");
        map.put("nonce_str", WXPayUtil.generateNonceStr());
        map.put("out_trade_no",out_trade_no);
        String xmlParam = WXPayUtil.generateSignedXml(map,"2ab9071b06b9f739b950ddb41db2690d");
        // 请求接口
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
        httpClient.setXmlParam(xmlParam);
        httpClient.setHttps(true);
        httpClient.post();
        // 处理返回结果
        String result  = httpClient.getContent();
        Map<String,String> resultMap = WXPayUtil.xmlToMap(result);
        String trade_state = resultMap.get("trade_state");
        return trade_state;
    }


    public static void main(String[] args) throws Exception{

        // 封装参数
        Map map = new HashMap();
        map.put("appid","wxab8acb865bb1637e");
        map.put("mch_id","11473623");
        map.put("nonce_str", WXPayUtil.generateNonceStr());
        map.put("body","这是一个商品");
        map.put("out_trade_no","123124234555");
        map.put("total_fee","1");
        map.put("spbill_create_ip","127.0.0.1");
        map.put("notify_url","127.0.0.1");
        map.put("trade_type","NATIVE");
        String xmlParam = WXPayUtil.generateSignedXml(map,"2ab9071b06b9f739b950ddb41db2690d");
        System.out.println(xmlParam);
        // 请求接口
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
        httpClient.setHttps(true);
        httpClient.setXmlParam(xmlParam);
        httpClient.post();

        String result = httpClient.getContent();
        // 处理返回结果
        System.out.println(result);
        // weixin://wxpay/bizpayurl?pr=afFJFIMzz

    }
}
